package com.ts.comm;

import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.Random;

/**
 * 字符串工具类
 * Created by xhc on 2017-3-22.
 */
public class SysString {
    /**
     * 判断是否为""或null
     */
    public static boolean isEmpty(Object obj) {
        if (obj == null) {
            return true;
        } else {
            return ("".equals(obj.toString().trim()));
        }
    }

    /**
     * 反转字符串
     *
     * @param str
     * @return String
     */
    public static String reverse(String str) {
        if (isEmpty(str)) {
            return "";
        }
        StringBuffer sb = new StringBuffer(str);
        sb.reverse();
        return sb.toString();
    }

    /**
     * 方法说明：查找字符存在的个数
     *
     * @param sSource
     * @param sFind
     * @return int
     */
    public static int findCount(String sSource, String sFind) {
        int iCount = 0;
        if (!SysString.isEmpty(sSource)) {
            int iIndex = sSource.indexOf(sFind, 0);
            while (iIndex != -1) {
                iCount++;
                iIndex = sSource.indexOf(sFind, iIndex + 1);
            }
        }
        return iCount;
    }

    /**
     * 把对象转换为String
     *
     * @param param
     * @return String
     */
    public static String toString(Object param) {
        if (param == null) {
            return "";
        } else {
            return param.toString();
        }
    }

    /**
     * 从hashmap中找key对应的值
     *
     * @param map
     * @param sKey
     * @return String
     */
    public static String getMapStr(Map<String, Object> map, String sKey) {
        if (map.get(sKey) == null) {
            return "";
        } else {
            return toString(map.get(sKey).toString());
        }
    }

    /**
     * 自动补全
     * leftPad("1", 3,"0"); 返回001
     * @param sSource 字符串
     * @param iLen    长度
     * @param str     要补的字符传
     * @return String
     */
    public static String lpad(String sSource, int iLen, String str) {
        String sValue = "";

        if (str.length() < iLen) {
            for (int i = 0; i < iLen - str.length(); i++) {
                sValue += str;
            }
        }
        return sValue + str;
    }

    /**
     * 自动补全
     * rPad("1", 3,"0"); 返回100
     * @param sSource 字符串
     * @param iLen    长度
     * @param str     要补的字符
     * @return String
     */
    public static String rPad(String sSource, int iLen, String str) {
        String sValue = "";
        if (str.length() < iLen) {
            for (int i = 0; i < iLen - str.length(); i++) {
                sValue += str;
            }
        }
        return str + sValue;
    }

    /**
     * 获取字节长度（按字节展示）
     * byteLength("你") 返回2
     * @param str
     * @return Integer
     */
    public static int byteLength(String str) {
        try {
            return str.getBytes("GBK").length;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 字节数组转化成String
     */
    public static String byte2hex(byte[] b) // 二行制转字符串
    {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1)
                hs = hs + "0" + stmp;
            else
                hs = hs + stmp;
            if (n < b.length - 1)
                hs = hs + ":";
        }
        return hs.toUpperCase();
    }



    /**
     * 循环生成字符串
     *
     * @param iCount 个数
     * @param str    字符串
     * @return String
     */
    public static String repeatStr(int iCount, String str) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < iCount; i++) {
            sb.append(str);
        }
        return sb.toString();
    }



    /**
     * 方法说明：去除重复
     *
     * @param sSource   字符串
     * @param sSplitStr 分割符
     * @return String
     */
    public static String removeRepeat(String sSource, String sSplitStr) {
        int iCount = 0;
        String[] cSource = null;
        String sRet = "";
        String sFindStr = "";
        cSource = sSource.split(sSplitStr);
        for (int i = 0; i < cSource.length; i++) {
            if (isEmpty(cSource[i])) {
                continue;
            }
            sFindStr = cSource[i] + sSplitStr;
            iCount = findCount(sSource, sFindStr);
            if (iCount > 1) {
                if (sRet.indexOf(sFindStr) != -1) {
                    continue;
                } else {
                    sRet += sFindStr;
                }
            } else {
                sRet += sFindStr;
            }

        }
        if (!isEmpty(sRet)) {
            sRet = sRet.replace(sSplitStr + sSplitStr, sSplitStr);
        }
        if (sRet.endsWith(sSplitStr)) {
            sRet = sRet.substring(0, sRet.length() - 1);
        }
        return sRet;
    }


    /**
     * 字符串编码转换工具类
     *
     * @param pString
     * @return String
     */
    public static String getGBK(String pString) {
        if (isEmpty(pString)) {
            return "";
        }
        try {
            pString = new String(pString.getBytes("ISO-8859-1"), "GBK");
        } catch (UnsupportedEncodingException e) {
            pString="";
        }
        return pString;
    }

    /**
     * 产生随机简体汉字
     *
     * @return String
     */
    public static String randomCN() {
        String outCharacter = null;
        int hightPos, lowPos;
        Random random = new Random();
        hightPos = (176 + Math.abs(random.nextInt(39)));
        lowPos = (161 + Math.abs(random.nextInt(93)));
        byte[] b = new byte[2];
        b[0] = (new Integer(hightPos).byteValue());
        b[1] = (new Integer(lowPos).byteValue());
        try {
            outCharacter = new String(b, "GBK");
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }
        return outCharacter;
    }
}
